<?php

/**
 * @author      :   TrongNV
 * @name        :   MatchExtendController
 * @copyright   :   Fpt Online
 * @todo        :   Index Controller
 */
class MatchextendController extends Zend_Controller_Action {

    private $session;
    private $intUserID;
    private $strFullname;
    private $strEmail;

    /**
     * init of controller
     */
    public function init() {
        //Disable layout
        $this->session = new Zend_Session_Namespace(SESSION_NAMESPACE);
        $this->intUserID = (int) $this->session->userid;
        $this->strFullname = (string) $this->session->user['fullname'];
        $this->strEmail = (string) $this->session->user['email'];
    }

    public function indexAction() {
        $matchID = intval($this->_request->getParam('mid', 0));

        //Set start offset
        $limit = LIMIT_OTHER_NEWS;

        // Models instance
        $matchModel = Match::getInstance();
        $matchExtendModel = MatchExtend::getInstance();
        //$newsModel = News::getInstance();
        // Get match detail
        $arrMatchDetail = $matchModel->getMatchDetailByIDs(array($matchID));
        $arrMatchDetail = $arrMatchDetail[0];
    
        $intTeam1ID = (int) $arrMatchDetail['Team1'];
        $intTeam2ID = (int) $arrMatchDetail['Team2'];

        //Get List ID ArticleID by Team1
        //$arrListIDNewsTeam1 = $newsModel->getListArticleByTeam($intTeam1ID, $limit);
        //Get List ID ArticleID by Team2
        //$arrListIDNewsTeam2 = $newsModel->getListArticleByTeam($intTeam2ID, $limit);
        //Get comment team1
        $arrCommentTeam1 = $matchExtendModel->getCommentByMatch($matchID, $intTeam1ID, 1);

        //Get comment team2
        $arrCommentTeam2 = $matchExtendModel->getCommentByMatch($matchID, $intTeam2ID, 1);

        //Get match extend detail
        $matchExtendDetail = $matchExtendModel->getDetailMatchExtend($matchID);
        
        //Get report match predict team1 win
        $arrReportPredictTeam1 = $matchExtendModel->getReportMatchPredict($matchID, 3);
        $arrReportPredictTeam1 = $arrReportPredictTeam1[0];

        //Get report match predict team2 win
        $arrReportPredictTeam2 = $matchExtendModel->getReportMatchPredict($matchID, 0);
        $arrReportPredictTeam2 = $arrReportPredictTeam2[0];

        //Get report match predict team drawn
        $arrReportPredictDrawn = $matchExtendModel->getReportMatchPredict($matchID, 1);
        $arrReportPredictDrawn = $arrReportPredictDrawn[0];

        //Get total match predict
        $arrTotalMatchPredict = $matchExtendModel->getTotalMatchPredict($matchID);
        $arrTotalMatchPredict = $arrTotalMatchPredict[0];

        //Get top match predict
        $arrTopMatchPredict = $matchExtendModel->getTopMatchPredict($matchID, 3);

        $this->view->headTitle(($matchExtendDetail[0]['Title']) ? $matchExtendDetail[0]['Title'] : '');
        $this->view->headMeta()->setName('description', ($matchExtendDetail[0]['Description']) ? $matchExtendDetail[0]['Description'] : '');
        $this->view->headMeta()->setName('keywords', ($matchExtendDetail[0]['Keyword']) ? $matchExtendDetail[0]['Keyword'] : '');

        // Assign to view
        $this->view->assign(array(
            'matchDetail' => $arrMatchDetail,
            'matchExtendDetail' => isset($matchExtendDetail[0]) ? $matchExtendDetail[0] : array(),
//            'arrListNewsTeam1' => $arrListIDNewsTeam1['data'],
//            'arrListNewsTeam2' => $arrListIDNewsTeam2['data'],
            'arrCommentTeam1' => $arrCommentTeam1,
            'arrCommentTeam2' => $arrCommentTeam2,
            'intReportPredictTeam1' => (int) $arrReportPredictTeam1['total'],
            'intReportPredictTeam2' => (int) $arrReportPredictTeam2['total'],
            'intReportPredictDrawn' => (int) $arrReportPredictDrawn['total'],
            'intTotalMatchPredict' => (int) ($arrTotalMatchPredict['total']) ? $arrTotalMatchPredict['total'] : 1,
            'arrTopMatchPredict' => $arrTopMatchPredict,
            'intMatchID' => $matchID,
            'intTeam1ID' => $intTeam1ID,
            'intTeam2ID' => $intTeam2ID,
            'intUserID' => $this->intUserID,
        ));
    }

    /**
     * Insert Match predict 
     * @author TrongNV
     */
    public function insertpredictAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $arrParams = $this->_request->getParams();
        $intMatchID = (int) ($arrParams['matchid']) ? $arrParams['matchid'] : 0;
        $params = array(
            'match_id' => $intMatchID,
            'mobile' => (string) $arrParams['mobile'],
            'userid' => (int) $this->intUserID,
            'goal_team1' => (int) $arrParams['goalteam1'],
            'goal_team2' => (int) $arrParams['goalteam2'],
            'num_people' => (int) $arrParams['numpeople'],
            'creation_time' => time(),
        );

        //Check matchExtendID
        if (!$intMatchID) {
            echo -1;
            return;
        }
        //Check input data
        if (empty($arrParams['mobile']) || empty($arrParams['numpeople'])) {
            echo -2;
            return;
        }
        //Insert predict
        $matchExtendModel = MatchExtend::getInstance();
        $arrReponse = $matchExtendModel->insertMatchPredict($params);
        echo Zend_Json::encode($arrReponse);
    }

    /**
     * Insert Comment Match Extend
     * @author TrongNV
     */
    public function insertcommentAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $arrParams = $this->_request->getParams();

        $intMatchID = (int) $arrParams['match_id'];
        $intTeamID = (int) $arrParams['teamid'];

        //Email
        if (!empty($this->strEmail)) {
            $strEmail = $this->strEmail;
        } else {
            $strEmail = $arrParams['email'];
        }

        //Fullname
        if (!empty($this->strFullname)) {
            $strFullName = $this->strFullname;
        } else {
            $strFullName = $arrParams['fullname'];
        }

        $params = array(
            'userid' => $this->intUserID,
            'match_id' => $intMatchID,
            'email' => (string) $strEmail,
            'fullname' => (string) $strFullName,
            'team_id' => $intTeamID,
            'content' => (string) $arrParams['content'],
            'creation_time' => time(),
        );

        //Check matchExtendID
        if (!$intMatchID) {
            echo -1;
            return;
        }
        //Check input data
        if (empty($intTeamID) || empty($arrParams['content'])) {
            echo -2;
            return;
        }
        //Insert predict
        $matchExtendModel = MatchExtend::getInstance();
        $arrReponse = $matchExtendModel->insertCommentMatchExtend($params);
        if ($arrReponse) {
            echo 1;
        }
    }

}

?>